package cn.breeze.pet.service.impl;

import cn.breeze.basic.service.impl.BaseServiceImpl;
import cn.breeze.pet.domain.PetType;
import cn.breeze.pet.mapper.PetTypeMapper;
import cn.breeze.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author breeze
 * @since 2022-07-16
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;

    /**
    * @Title: loadPetTypeTree
    * @Description: 宠物类型文档树
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/16 16:21
    * @Parameters: []
    * @Return java.util.List<cn.breeze.org.domain.PetType>
    */
    @Override
    public List<PetType> loadPetTypeTree() {
        List<PetType> deptAll = petTypeMapper.loadAll();
        Map<Long, PetType> collect = deptAll.stream().collect(Collectors.toMap(PetType::getId, item -> item));
        List<PetType> petTypes = new LinkedList<>();
        for (PetType first : deptAll){
            if (first.getPid() == null){
                petTypes.add(first);
            } else {
                PetType department = collect.get(first.getPid());
                if (department != null){
                    department.getChildren().add(first);
                }
            }
        }
        return petTypes;
    }


}
